<!-- src/views/PaperList.vue -->
<template>
  <div>
    <van-pull-refresh v-model="loading" @refresh="loadPapers" class="paper-list">
      <van-list v-model:loading="listLoading" :finished="finished" @load="onLoad">
        <van-cell v-for="paper in papers" :key="paper.id" :title="paper.title"
          :label="`题目数: ${paper.questionCount} | 难度: ${paper.difficulty}`" @click="startExam(paper)">
          <template #right-icon>
            <van-tag type="primary">{{ paper.score }}分</van-tag>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import service from '@/api/request'

// 定义 Paper 类型
interface Paper {
  id: number;
  title: string;
  questionCount: number;
  difficulty: string;
  score: number;
}


const loading = ref(false)
const listLoading = ref(false)
const finished = ref(false)

const papers = ref<Paper[]>([])

const onLoad = async () => {
  return;
}

const startExam = (paper: Paper) => {
  // 跳转到试卷详情页面
  window.location.href = `/exam/${paper.id}`
}
const loadPapers = async () => {
  try {
    service.post('/test').then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
    const res = {
      data: [
        {
          id: 1
        }
      ]
    }

    // 检查返回数据是否符合预期
    // if (!res || !res.data || !Array.isArray(res.data)) {
    //   console.error('API返回数据格式错误:', res);
    //   return;
    // }

    // 更新papers数据
    papers.value = res.data.map((item: any) => {
      // 确保每个paper对象包含所有必要字段
      return {
        id: item.id || 0,
        title: item.title || '未知标题',
        questionCount: item.questionCount || 0,
        difficulty: item.difficulty || '未知难度',
        score: item.score || 0,
      };
    });

    loading.value = false;
    finished.value = true;
    console.log(finished)
  } catch (error) {
    console.error('加载试卷失败:', error);
    loading.value = false;
    finished.value = true;
  }
}
</script>

<style>
.paper-list {
  height: 100vh;
}
</style>